-- 指剑为雨
local zhijianweiyu = fk.CreateSkill {
    name = "mcdh_zhijianweiyu",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

zhijianweiyu:addEffect('active',{
    card_num = 1,
    target_num = 1,
    can_use = function(self, player)
        return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    target_filter = function(self, player,to_select, selected)
        return #selected == 0 and to_select ~= player
    end,
    card_filter = function(self,player, to_select, selected)
        return #selected == 0
    end,
    on_use = function(self, room, effect)
        local target = effect.tos[1]
        target:addToPile("mcdh_lu", effect.cards, true, self.name)
    end
})

zhijianweiyu:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(self)  then return false end
        local room = player.room
        local move_event = room.logic:getCurrentEvent()
        local parent_event = move_event.parent
        local card_ids = {}
        local result = {}
        if parent_event ~= nil then
            if parent_event.event == GameEvent.UseCard or parent_event.event == GameEvent.RespondCard then
                local parent_data = parent_event.data[1]
                if parent_data.from == player.id then
                    card_ids = room:getSubcardsByRule(parent_data.card)
                end
            elseif parent_event.event == GameEvent.Pindian then
                local pindianData = parent_event.data[1]
                if pindianData.from == player then
                    card_ids = room:getSubcardsByRule(pindianData.fromCard)
                else
                    for toId, result in pairs(pindianData.results) do
                        if player.id == toId then
                            card_ids = room:getSubcardsByRule(result.toCard)
                            break
                        end
                    end
                end
            end
        end
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                if move.from == player.id then
                    for _, info in ipairs(move.moveInfo) do
                        if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                                Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
                            table.insertIfNeed(result,info.cardId)
                        end
                    end
                elseif #card_ids > 0 then
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) and
                                Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
                            table.insertIfNeed(result,info.cardId)
                        end
                    end
                end
            end
        end
        result = table.filter(result,function(id) return room:getCardArea(id) == Card.DiscardPile  end)
        if #result > 0 then
            self.cost_data = result
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local targetPlayer = room:askToChoosePlayers(player, {
            targets = room:getOtherPlayers(player),
            min_num = 1,
            max_num = 1,
            prompt = "#zhijianweiyu-choose",
            skill_name = self.name,
            cancelable = false
          })[1]
        targetPlayer:addToPile("mcdh_lu", self.cost_data, true, self.name)
    end
})


Fk:loadTranslationTable {
    ["mchd_zhijianweiyu"] = "指雨為劍",
    [":mchd_zhijianweiyu"] = "出牌阶段限一次，你可以将一张牌置于其他角色的武将牌上，称为「露」。当你的武器牌进入弃牌堆后，你可以将此牌置为「露」。",
    ["#zhijianweiyu-choose"] = "指雨為劍：将武器牌置于其他角色的武将牌上，称为「露」",
    ["mcdh_lu"] = "露",
}

return zhijianweiyu